package com.ruoyi.scheduleTask

import com.ruoyi.app.common.format
import com.ruoyi.back.enums.SendUserTypeEnum
import com.ruoyi.back.service.SystemNoticeParam
import com.ruoyi.back.service.SystemNoticeService
import com.ruoyi.back.service.UserSystemNoticeMessageService
import org.quartz.DisallowConcurrentExecution
import org.quartz.Job
import org.quartz.JobExecutionContext
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.*

@Component
@DisallowConcurrentExecution
class SendUserMessageJob : Job {

    private val log = LoggerFactory.getLogger(SendUserMessageJob::class.java)

    @Autowired
    private lateinit var userSystemNoticeMessageService: UserSystemNoticeMessageService

    @Autowired
    private lateinit var systemNoticeService: SystemNoticeService

    override fun execute(p0: JobExecutionContext) {
        log.info("定时任务开始执行 ${Date().format()}")
        val systemNoticeParam = p0.mergedJobDataMap["param"] as SystemNoticeParam
        val userType = p0.mergedJobDataMap["type"] as SendUserTypeEnum
        userSystemNoticeMessageService.sendUserSystemNoticeMessage(userType, systemNoticeParam)
        systemNoticeService.getById(systemNoticeParam.systemNoticeId)
            ?.run {
                systemNoticeService.updateById(this.apply {
                    sendTime = Date()
                    send = true
                })
            }
    }
}